package exam.model.dao;

import java.util.HashMap;
import java.util.List;

import exam.model.entity.TExaminationEntity;
import exam.system.dao.AbstractDAO;

public class TExaminationDAO extends AbstractDAO<TExaminationEntity>{
	
	public TExaminationDAO() {
		super(TExaminationEntity.class);
	}
	
	public List<TExaminationEntity> selectListExamination() {
		String query = "select e from TExaminationEntity e left outer join e.subQuestionEntity";
		return super.selectList(query);
	}
	

	public TExaminationEntity selectByPk(long id) {
		String query = "select e from TExaminationEntity e left outer join e.subQuestionEntity where e.examId=:examId";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("examId", id);
		return super.jdbcManager.executeByQueryParams(query, params).getSingleResult();
	}
	
	public TExaminationEntity selectByCd(String cd) {
		String query = "select e from TExaminationEntity e where e.examCd=:examCd";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("examCd", cd);
		return super.jdbcManager.executeByQueryParams(query, params).getSingleResult();
	}
	
	/**
	 * insertExamination
	 * @param tExaminationEntity
	 * @return int
	 */
	public int insertExamination(TExaminationEntity tExaminationEntity) {
		return super.insert(tExaminationEntity);
	}
	
	/**
	 * updateExamination
	 * @param tExaminationEntity
	 * @return
	 */
	public int updateExamination(TExaminationEntity tExaminationEntity) {
		String query = "update TExaminationEntity e set e.examCd=:examCd, e.examTitle=:examTitle, " +
				"e.questNum=:questNum, e.examTime=:examTime, e.status=:status, e.subQuestId=:subQuestId," +
				"e.updateDate=:updateDate where e.examId=:examId";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("examCd", tExaminationEntity.getExamCd());
		params.put("examTitle", tExaminationEntity.getExamTitle());
		params.put("questNum", tExaminationEntity.getQuestNum());
		params.put("examTime", tExaminationEntity.getExamTime());
		params.put("status", tExaminationEntity.getStatus());
		params.put("subQuestId", tExaminationEntity.getSubQuestId());
		params.put("updateDate", tExaminationEntity.getUpdateDate());
		params.put("examId", tExaminationEntity.getExamId());
		return jdbcManager.executeByQueryParams(query, params).getInt();
	}
	
	/**
	 * deleteExamination: cap nhat 21/5/2012
	 * @param examId
	 * @return int
	 */
	public void deleteExamination(long examId) {
//		String query = "delete from TExaminationEntity e where e.examId=:examId";
//		HashMap<String, Object> params = new HashMap<String, Object>();
//		params.put("examId", examId);
		super.delete(examId);
	}
}
